﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Entity;

namespace Job.Web
{
    public class MySession
    {
        //保存在Seesion中的后台管理员关键字
        private string KEY_ADMIN_USER = "admin_user";   


        //保存后台管理员的信息到Session中
        public void AddAdminUser2Session(JobUser user) 
        {
            if (null == user) return;
            HttpContext.Current.Session.Remove(KEY_ADMIN_USER);
            HttpContext.Current.Session.Add(KEY_ADMIN_USER,user);
        }
        
        //获得当前登录的后台管理员信息
        public JobUser GetAdminUserFromSession()
        {
            string key;
            for( int i = 0 ; i < HttpContext.Current.Session.Keys.Count ; i++ )
            {
                key = (string)HttpContext.Current.Session.Keys[i];
                if (key.Equals(KEY_ADMIN_USER))
                {
                    return (JobUser)HttpContext.Current.Session[KEY_ADMIN_USER];
                }
            }
            return null;
        }
        //将后台管理员的信息从Session中删除
        public void RemoveAdminUserFromSession()
        {
            string key;
            for (int i = 0; i < HttpContext.Current.Session.Keys.Count; i++)
            {
                key = (string)HttpContext.Current.Session.Keys[i];
                if (key.Equals(KEY_ADMIN_USER))
                {
                    HttpContext.Current.Session.Remove(KEY_ADMIN_USER);
                }
            }
        }
    }
}
